//
//  UIDevice.m
//  WebPDA
//
//  Created by Justin Yip on 8/6/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "UIDevice+Connectivity.h"
#import <SystemConfiguration/SCNetworkReachability.h>

#define EXTERNAL_HOST @"www.baidu.com"

@implementation UIDevice (DeviceConnectivity)
+(BOOL)cellularConnected {
	SCNetworkReachabilityFlags flags = 0;
	SCNetworkReachabilityRef netReachability;
	netReachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [EXTERNAL_HOST UTF8String]);
	if (netReachability) {
		SCNetworkReachabilityGetFlags(netReachability, &flags);
		CFRelease(netReachability);
	}
	if (flags & kSCNetworkReachabilityFlagsIsWWAN) {
		return YES;
	} else {
		return NO;
	}

}
+(BOOL)networkConnected {
	SCNetworkReachabilityFlags flags = 0;
	SCNetworkReachabilityRef netReachability;
	BOOL retrievedFlags = NO;
	netReachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [EXTERNAL_HOST UTF8String]);
	if (netReachability) {
		retrievedFlags = SCNetworkReachabilityGetFlags(netReachability, &flags);
		CFRelease(netReachability);
	}
	if (!retrievedFlags || !flags) {
		return NO;
	}else {
		return YES;
	}
}
+(BOOL)wiFiConnected {
	if ([self cellularConnected]) {
		return NO;
	}else {
		return [self networkConnected];
	}
}
@end
